var common = new function () {
	
	this.windowClose = function(){
		window.opener.location.reload(true);
		window.close();
		
	};
	//收藏網址
    this.addFavorite = function () {
       if (document.all){
	      window.external.addFavorite('http://dream:8080','*****');
	   }else if (window.sidebar){
	      window.sidebar.addPanel('***','http://dream:8080', "");
	   }
        return false;
    };
	this.root = "/dream";
	this.common_image_width = 140;
	this.common_image_height = 140;
	
	this.checkUserImage = function (obj){
		return this.checkImageSize(obj,this.common_image_width,this.common_image_height);
	};
    this.checkImageSize = function(obj,w,h){
    	
    	   var $file = $(obj);
           var fileName = $file.val();
           if(!fileName){
        	   common.modalGlobalMessage("请选择图片!","错误"); 
        	   return false;
           }
           var i= fileName.lastIndexOf('.');
           var len= fileName.length;
           var extEndName= fileName.substring(i+1,len);
           var extName="BMP,JPG,JPEG,PNG";//首先对格式进行验证
           if(extName.indexOf(extEndName.toUpperCase())==-1){
             common.modalGlobalMessage("*您只能输入"+extName+"格式的文件","错误"); 
             return false;
           }else{
               var url,image;
               if(obj.files){
                  url= window.URL.createObjectURL(obj.files[0]);
               }else{
                  url=obj.value;
                  url="file:///"+url;
               }
               image=new Image();
               image.src=url;
               image.onload=function(){
	        	   if(image.width>w || image.height> h ){
	        		   common.modalGlobalMessage("文件大小不对,最大140*140!","错误"); 
	        		   $file.val("");
	        	   }
               };
           }
           return true;
    };
    this.checkFileType = function(fileName,extName){
    	
  	  var i= fileName.lastIndexOf('.');
      var len= fileName.length;
      var extEndName= fileName.substring(i+1,len);
      
      if(extName.indexOf(extEndName.toUpperCase())==-1){
          common.modalGlobalMessage("*您只能输入"+extName+"格式的文件","错误"); 
          return false;
        }
        return true;
    };
    
    this.checkFileImage = function( fileName ){
          var extName="BMP,JPG,JPEG,PNG";//首先对格式进行验证
          return common.checkFileType(fileName,extName);
    };
    this.checkFileText = function( fileName ){
    	
        var extName="TXT,DOC,DOCX";//首先对格式进行验证
        return common.checkFileType(fileName,extName);
    };
    this.isEmpty = function (val) {
        val = $.trim(val);
        
        return (val == "");

    };
    this.isIllegalLen = function (val, len) {
        val = $.trim(val);
        return (val.length != len);
    };
    this.isIllegalMinLen = function (val, min) {
        val = $.trim(val);
        return (val.length < min);

    };
    this.isIllegalMaxLen = function (val,max) {
        val = $.trim(val);
        return  (val.length > max);

    };
    this.isIllegalRangeLen = function (val, min ,max) {
        val = $.trim(val);
        return (val.length < min) || (val.length > max);

    };
    this.isIllegalMobile = function (mobile) {
        var reg = RegExp("1[0-9]{10}$");
        return !reg.test(mobile);

    };
    this.isIllegalPhone = function (phone) {
        var reg = RegExp("(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?");
        return !reg.test(phone);

    };
    this.isNotNumberic = function (val) {
        val = $.trim(val);
        var reg = RegExp("^[0-9]+$");
        return !reg.test(val);
    };
    this.isNotInteger = function (val) {
        val = $.trim(val);
        var reg = RegExp("^[1-9][0-9]*$");
        return !reg.test(val);
    };
    
    
    this.isNotNumOrChar = function( val ){
    	
        val = $.trim(val);
        var reg = RegExp("^[a-zA-Z0-9]+$");
        return !reg.test(val);
        
    };
    
    this.isNotFloat = function (val) {
        val = $.trim(val);
        var reg = RegExp("^[0-9]+(.[0-9]{1,2})?$");
        return !reg.test(val);
    };
    this.isIllegalDate = function (val) {
        val = $.trim(val);
        var reg = RegExp("^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$");
        var format = !reg.test(val);
        if(format){
        	return true;
        }
        var year = val.substr(0, 4);
        var month = val.substr(5, 2);
        var day = val.substr(8, 2);
        var isLoop = false;
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            isLoop = true;
        }
        var months = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        if (isLoop == true)
            months[1] = 29;
        if (month < 1 || month > 12)
            return true;
        else {
            var days = months[month - 1];
            if (day < 1 || day > days)
                return true;
        }
        return false;
    };
    this.isIllegalEmail = function (val) {
        val = $.trim(val);
        var reg = RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
        return !reg.test(val);
    };
    this.showClientErrorTip = function (id,msg) {
    	
        if (msg){
        	var options={
    				animation:true,
    				title:msg,
    				placement:'right'
    		};
        	var $p = $("#"+id).parent();
        	if($p.is("td")){
        		$p.addClass("error");
        	}else{
        		$p.parent().addClass("error");
        	}
        	$('#'+id).tooltip("destroy");
        	$('#'+id).tooltip(options);
        } else {
        	common.destoryTip(id);
        }
    };
    this.destoryTip = function(id){
    	var $p = $("#"+id).parent();
    	if($p.is("td")){
    		$p.removeClass("error");
    	}else{
    		$p.parent().removeClass("error");
    	}
    	$('#'+id).tooltip("destroy");
    };
    this.modalGlobalMessage = function(msg,title){
    	if(!msg){return;}
    	var myTitle = "系统消息";
    	if(title){ myTitle = title; }
    	var msgHtml = '<div id="globalMessage" class="modal fade">' +
				    	  '<div class="modal-dialog">' +
						        '<div class="modal-content">' +
						          '<div class="modal-header">' +
						            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
						            '<h4 class="modal-title">'+myTitle+'</h4>' +
						          '</div>' +
						          '<div class="modal-body">' +
						            '<p>'+msg+'</p>' +
						          '</div>' +
						          '<div class="modal-footer">' +
						            '<button type="button" class="btn btn-default btn-small" data-dismiss="modal">关闭</button>' +
						          '</div>' +
						        '</div>' +
						      '</div>' +
						 '</div>' ;
      $('#globalMessage').remove();
     $("body").append(msgHtml);
	 $('#globalMessage').modal('show');
    };
    this.closeGlobalMessage = function(){
    	common.closeModal("globalMessage");
    };
    this.moalYesNoDialog = function(msg,title,yesfun){
    	if(!msg){return;}
    	var myTitle = "系统消息";
    	if(title){ myTitle = title; }
    	
    	var msgHtml = '<div id="yesNoDialog" class="modal fade">' +
				    	  '<div class="modal-dialog">' +
						        '<div class="modal-content">' +
						          '<div class="modal-header">' +
						            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
						            '<h4 class="modal-title">'+myTitle+'</h4>' +
						          '</div>' +
						          '<div class="modal-body">' +
						            '<div class="form-horizontal">'+msg+'</div>' +
						          '</div>' +
						          '<div class="modal-footer">' +
						            '<button type="button" class="btn btn-default btn-small" data-dismiss="modal">取消</button>' +
						             '<button type="button" class="btn btn-success btn-small" onclick="'+yesfun+'">确定</button>' +
						          '</div>' +
						        '</div>' +
						      '</div>' +
						 '</div>' ;
     $('#yesNoDialog').remove();
     $("body").append(msgHtml);
	 $('#yesNoDialog').modal('show');
    };
    this.closeYesNoDialog = function(){
    	common.closeModal("yesNoDialog");
    };
    this.progressDialog = function( text ){
    	
    	
    	var htmlDialog = '<div id="progressDialog" class="modal fade"  style="width:140px;height:140px;padding:0px" >' +
		  	  			'<div class="modal-dialog" style="width:140px;height:140px;padding:0px">' +
					          '<div class="modal-body" style="width:140px;height:140px;padding:0px;"><p class="text-center">' + text + '</p>'+
					          '<div class="modal-progress"></div>' +
					        '</div>' +
					     '</div>' +
				      '</div>' ;
    	 $('#progressDialog').remove();
    	$("body").append(htmlDialog);
    	var options = {backdrop:false,keyboard:false};
	    $('#progressDialog').modal(options);
    };
    this.closeProgressDialog = function(){
    	common.closeModal("progressDialog");
    };
    this.modalDialog = function(uri,htmlContent,htmlButton,title){
    	if(!msg){return;}
    	var myTitle = "系统消息";
    	if(title){ myTitle = title; }
		$.post(uri,
			function( html ){
						var htmlDialog = '<div id="modalDialog" class="modal">' +
				    	  '<div class="modal-dialog">' +
						        '<div class="modal-content">' +
						          '<div class="modal-header">' +
						            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
						            '<h4 class="modal-title">'+myTitle+'</h4>' +
						          '</div>' +
						          '<div class="modal-body">' +
						            '<p>'+htmlContent+'</p>' +
						          '</div>' +
						          '<div class="modal-footer">' +
						            '<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>' + htmlButton +
						          '</div>' +
						        '</div>' +
						      '</div>' +
						 '</div>' ;
					  $('#modalDialog').remove();
					  $("body").append(htmlDialog);
			          $('#modalDialog').modal('show');
		 });
	};
    this.closeModalDialog = function(){
    	common.closeModal("modalDialog");
    };
   this.closeModal = function (id){
	   $('#'+id).modal('hide');
   };
   this.ajaxUriDialog = function(uri,title,okFun){
	   $.ajax({
   	    type:"post",
   	    async:false,
   	    url:uri,
   	    success:function(html){
   	    	common.moalYesNoDialog(html,title,okFun);
   	    }
	  });
   };
   this.ajaxCheck = function(uri,formId,errMsg,succMsg,succFun){
	 var result = false;
    	$.ajax({
    	    type:"post",
    	    async:false,
    	    url:uri,
    	    data:$("#"+formId).serialize(),
    	    success:function(data){
    	    	 if(data){
    	    		 result = true;
        			 // 成功
        			 if(succMsg){
        				 common.moalYesNoDialog(succMsg,"成功",succFun);
        			 }else{
        				 eval(succFun);
        			 }
        		 }else {
        			 common.modalGlobalMessage(errMsg,"错误"); 
        		 }
    	    }
    	  });
    	  return result;
    };
    this.ajaxQuery =  function(uri){
    	$("#pageIndex").val(1);
    	common.ajaxPagingQuery(uri);
    };
    this.ajaxPagingQuery =  function(uri){
    	
    	var formData = $("#queryForm").serialize() + "&" +  $("#pagingForm").serialize();
    	common.progressDialog("...查询中...");
    	$.post(uri, formData,function(html){
    		$("#dataList").html(html);
    		common.closeProgressDialog();
    	});
    };
    this.ajaxSubmit = function( url ,formId,errMsg,succMsg,succFun){
    	
    	var formData = $("#"+formId).serialize();
    	common.ajaxSubmitWithData(url,formData,errMsg,succMsg,succFun);
    };
    this.ajaxSubmitWithData = function( url ,formData,errMsg,succMsg,succFun){
    	common.progressDialog("...提交中...");
    	$.post(url, formData, function(result) {
    		common.closeProgressDialog();
    		 if(result.type == "0"){
    			 var mvcValidtorResult = result.content;
    			 for(var key in mvcValidtorResult.fieldErrors){
    				 common.showClientErrorTip(key,mvcValidtorResult.fieldErrors[key]);
    			 }
    			 var globalMsg = globalMessage.globalMessage;
    			 if(globalMsg){
    				 common.modalGlobalMessage(globalMsg,"错误");  
    			 }
    		 } else if(result.type == "1"){
    			 common.modalGlobalMessage(errMsg,"错误"); 
    		 }else if(result.type == "2"){
    			 common.modalGlobalMessage("系统出现异常了","系统异常");  
    		 }else if(result.type == "3"){
    			 if(succMsg){
    				 common.moalYesNoDialog(succMsg,"成功",succFun);
    			 }else{
    				 eval(succFun);
    			 }
    		 }else{
    			 common.modalGlobalMessage("系统出现异常了","系统异常");  
    		 }
    	},"json");
    };
    this.getCookie = function(cookieName){
    	var cookies = document.cookie.split("; ");
    	for(var i=0; i<cookies.length; i++){
	    	var cookie = cookies[i].split("=");
	    	if(cookieName == cookie[0] ){
	    		return cookie[1];
	    	}
    	}
    	return "";
   };
   this.reload = function (uri){
	   window.location.replace(uri);
   };
   this.replaceAll = function(str,oldStr,newStr){
	   
	   return str.replace(new RegExp(oldStr,"g"),newStr);   
   };
	
	this.fieldTmp ="<p><label>@0:</label><span class='field'><input type='hidden'  name='@1' value='@0' class='input-xSmall' />" +
				"<input type='text'  name='@2' class='input-xSmall' /></span></p>";
	
	this.fieldTmpTr ="<tr><th>@0:</th><td><input type='hidden'  name='@1' value='@0' class='input-xSmall' />" +
	"<input type='text'  name='@2' class='input-xSmall' /></td></tr>";
	
	
	this.reset = function(){
		$(".checker > span").removeClass('checked');
		$("input[type='radio'] input[type='checkbox']").prop('checked', false);
	};
	
	this.print = function(){
		$("#printArea").printArea();
	};
	this.printById = function( id ){
		$("#"+id).printArea();
	};
	this.displayNav = function(headerId,navId){
		
		$("li[id^='h']").each(function(){
			$(this).removeClass("active");
		});
		$("#"+headerId).addClass("active");

		$("li[id^='n']").each(function(){
			
			var meId = $(this).attr("id");
			if(meId == navId){
				$(this).parent("ul").parent("li").addClass("open");
				$(this).children("a").css("color","#04C");
				return false;
			}
			
		});
	};
};